package com.campus_assistant.controller;

import com.campus_assistant.common.AIParam;
import com.campus_assistant.service.db1.CoursesService;
import com.campus_assistant.service.db1.CoursesServiceImpl;
import com.campus_assistant.service.db1.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

@RestController
public class CentreController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private CoursesService coursesService;

    @RequestMapping("/centre")//前端传递的text参数
    public RedirectView judge(@RequestBody AIParam aiParam, RedirectAttributes attributes) {//中枢判断方法，确定之后进入的controller
        String text=aiParam.getText();
        System.out.println("原始输入内容为："+text);
        char[] textChars = text.toCharArray();
        for (int i = 0; i < textChars.length - 2; i++) {
            //查询功能
                if (textChars[i] == '查' && textChars[i + 1] == '询') {
                    //学生表
                    if(textChars[i+2] == '学' && textChars[i + 3] == '生'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'){
                        //查询所有学生
                        if (textChars[i + 6] == '所' && textChars[i + 7] == '有' && textChars[i + 8] == '学' && textChars[i + 9] == '生') {
                            return new RedirectView("/selectAllStudent");
                        } else {//查询某个学生
                            char[] name1 = new char[20];
                            int n = 0;
                            for (int j = i + 6; j < textChars.length; j++) {
                                name1[n++] = textChars[j];
                            }
                            String name2 = String.valueOf(name1);//将char数组转为String
                            String result = name2.substring(0, n);//将名字截取出来
                            System.out.println("姓名为：" + result);
                            attributes.addAttribute("result",result);
                            return new RedirectView("/getStudentByName");
                        }
                    }
                    //课程表
                    if(textChars[i+2] == '课' && textChars[i + 3] == '程'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'){
                        //查询所有课程
                        if (textChars[i + 6] == '所' && textChars[i + 7] == '有' && textChars[i + 8] == '课' && textChars[i + 9] == '程') {
                            return new RedirectView("/selectAllCourses");
                        } else {
                            for (int j=i+6;j< textChars.length;j++){
                                //查询某个老师的任课情况
                                if(textChars[j] == '的' && textChars[j + 1] == '任' && textChars[j + 2] == '课' && textChars[j + 3] == '情'&&textChars[j+4] == '况'){
                                    int n=i+6;//暂存
                                    int m=0;
                                    char[] teacherName1=new char[20];
                                    String teacherName2=null;
                                    while (j>n){
                                        teacherName1[m++]=textChars[n];
                                        n++;
                                    }
                                    teacherName2=String.valueOf(teacherName1).trim();
                                    System.out.println(teacherName2);
                                    attributes.addAttribute("teacherName",teacherName2);
                                    return new RedirectView("/selectCoursesByTeacherName");
                                }
                                //查询某学科的任课老师
                                if(textChars[j] == '学' && textChars[j + 1] == '科' && textChars[j + 2] == '的' && textChars[j + 3] == '任'&&textChars[j+4] == '课'&&textChars[j+5] == '老'&&textChars[j+6] == '师'){
                                    int n=i+6;//暂存
                                    int m=0;
                                    char[] coursesName1=new char[20];
                                    String coursesName2=null;
                                    while (j>n){
                                        coursesName1[m++]=textChars[n];
                                        n++;
                                    }
                                    coursesName2=String.valueOf(coursesName1).trim();
                                    System.out.println(coursesName2);
                                    attributes.addAttribute("coursesName",coursesName2);
                                    return new RedirectView("/selectTeacherNameByCourses");
                                }
                            }
                        }
                    }
                    //成绩表
                    if(textChars[i+2] == '成' && textChars[i + 3] == '绩'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中') {
                        //查询所有成绩
                        if (textChars[i + 6] == '所' && textChars[i + 7] == '有' && textChars[i + 8] == '成' && textChars[i + 9] == '绩') {
                            return new RedirectView("/selectStudentScoresAll");
                        }else{
                            for (int j=i+6;j< textChars.length;j++){
                                //查询某个学生的成绩
                                if(textChars[j] == '的' && textChars[j + 1] == '成' && textChars[j + 2] == '绩'){
                                    int n=i+6;//暂存
                                    int m=0;
                                    char[] studentName1=new char[20];
                                    String studentName2=null;
                                    while (j>n){
                                        studentName1[m++]=textChars[n];
                                        n++;
                                    }
                                    studentName2=String.valueOf(studentName1).trim();
                                    System.out.println("学生名为："+studentName2);
                                    attributes.addAttribute("studentName",studentName2);
                                    return new RedirectView("/selectStudentScoreByStudentName");
                                }
                                //查询某学科的所有成绩
                                if(textChars[j] == '学' && textChars[j + 1] == '科' && textChars[j + 2] == '的' && textChars[j + 3] == '所'&&textChars[j+4] == '有'&&textChars[j+5] == '成'&&textChars[j+6] == '绩'){
                                    System.out.println("查询某学科的所有成绩");
                                    int n=i+6;//暂存
                                    int m=0;
                                    char[] coursesName1=new char[20];
                                    String coursesName2=null;
                                    while (j>n){
                                        coursesName1[m++]=textChars[n];
                                        n++;
                                    }
                                    coursesName2=String.valueOf(coursesName1).trim();
                                    attributes.addAttribute("courseName",coursesName2);
                                    return new RedirectView("/selectStudentScoreByCourseName");
                                }
                                //查询某老师任教课程的所有成绩
                                if(textChars[j] == '老' && textChars[j + 1] == '师' && textChars[j + 2] == '任' && textChars[j + 3] == '教'&&textChars[j+4] == '课'&&textChars[j+5] == '程'&&textChars[j+6] == '的'&&textChars[j+7] == '成'&&textChars[j+8] == '绩'){
                                    int n=i+6;//暂存
                                    int m=0;
                                    char[] teacherName1=new char[20];
                                    String teacherName2=null;
                                    while (j>n){
                                        teacherName1[m++]=textChars[n];
                                        n++;
                                    }
                                    teacherName2=String.valueOf(teacherName1).trim();
                                    attributes.addAttribute("teacherName",teacherName2);
                                    return new RedirectView("/selectStudentScoreByTeacherName");
                                }
                            }
                        }
                    }
                    //添加功能
                }else if(textChars[i] == '添' && textChars[i + 1] == '加'){
                    System.out.println("已进入添加模块");
                    //添加学生
                    if (textChars[i+2]=='到'&&textChars[i+3] == '学' && textChars[i + 4] == '生'&& textChars[i + 5] == '表'&& textChars[i + 6] == '中'&&(textChars[i + 7] == ':'||textChars[i + 7] == '：')){
                        String message = getMessage(textChars, i+8);
                        attributes.addAttribute("result",message);

                        return new RedirectView("/addStudent1");
                    }
                    //添加课程
                    if (textChars[i+2]=='到'&&textChars[i+3] == '课' && textChars[i + 4] == '程'&& textChars[i + 5] == '表'&& textChars[i + 6] == '中'&&(textChars[i + 7] == ':'||textChars[i + 7] == '：')){
                        String message = getMessage(textChars, i+8);
                        attributes.addAttribute("result",message);
                        return new RedirectView("/addCourses");
                    }
                    //修改功能
                }else if(textChars[i] == '修' && textChars[i + 1] == '改'){
                    //修改学生
                    if (textChars[i+2] == '学' && textChars[i + 3] == '生'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        int n=i+7;//暂存
                        int m=0;
                        char[] studentName1=new char[20];
                        String studentName2=null;
                        while (n< textChars.length){
                            studentName1[m]=textChars[n];
                            m++;
                            n++;
                        }
                        studentName2=String.valueOf(studentName1).trim();
                        attributes.addAttribute("studentName",studentName2);
                        return new RedirectView("/updateStudent3");
                    }
                    //修改课程
                    if (textChars[i+2] == '课' && textChars[i + 3] == '程'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        int n=i+7;//暂存
                        int m=0;
                        char[] courseName1=new char[20];
                        String courseName2=null;
                        while (n< textChars.length){
                            courseName1[m]=textChars[n];
                            m++;
                            n++;
                        }
                        courseName2=String.valueOf(courseName1).trim();
                        attributes.addAttribute("courseName",courseName2);
                        return new RedirectView("/updateCourse2");
                    }
                    //修改成绩
                    if (textChars[i+2] == '课' && textChars[i + 3] == '程'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        int n=i+7;//暂存
                        int m=0;
                        char[] studentName1=new char[20];
                        String studentName2=null;
                        while (n< textChars.length){
                            studentName1[m]=textChars[n];
                            m++;
                            n++;
                        }
                        studentName2=String.valueOf(studentName1).trim();
                        attributes.addAttribute("studentName",studentName2);
                        return new RedirectView("/updateScore");
                    }
                    //删除功能
                }else if(textChars[i] == '删' && textChars[i + 1] == '除'){
                    if (textChars[i+2] == '学' && textChars[i + 3] == '生'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        char[] name1 = new char[20];
                        int n = 0;
                        for (int j = i + 7; j < textChars.length; j++) {
                            name1[n++] = textChars[j];
                        }
                        String name2 = String.valueOf(name1);//将char数组转为String
                        String result = name2.substring(0, n);//将名字截取出来
                        attributes.addAttribute("result",result);
                        return new RedirectView("/deleteStudent1");
                    }
                    //通过课程名和老师名删除课程表的内容
                    if (textChars[i+2] == '课' && textChars[i + 3] == '程'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        char[] courseName1 = new char[20];
                        char[] teacherName1 = new char[20];
                        int n = 0;
                        int m=0;
                        int temp1=i+7;

                        for (int j = i + 7; j < textChars.length; j++) {
                            if(textChars[j]=='课'&&textChars[j+1]=='程'){
                                while (temp1<j){
                                    courseName1[n++]=textChars[temp1];
                                    temp1++;
                                }
                                j=j+3;
                                while (j<textChars.length){
                                    teacherName1[m++]=textChars[j];
                                    j++;
                                }
                            }
                        }
                        String courseName2 = String.valueOf(courseName1);//将char数组转为String
                        String courseName=courseName2.substring(0,n);
                        System.out.println("课程名为："+courseName);
                        String teacherName2 = String.valueOf(teacherName1);//将char数组转为String
                        String teacherName=teacherName2.substring(0,m);
                        System.out.println("老师名为："+teacherName);
                        attributes.addAttribute("courseName",courseName);
                        attributes.addAttribute("teacherName",teacherName);
                        return new RedirectView("/deleteCourse");
                    }
                    ////通过课程名和学生名删除成绩表的内容
                    if (textChars[i+2] == '成' && textChars[i + 3] == '绩'&& textChars[i + 4] == '表'&& textChars[i + 5] == '中'&&(textChars[i + 6] == ':'||textChars[i + 6] == '：')){
                        char[] courseName1 = new char[20];
                        char[] studentName1 = new char[20];
                        int n = 0;
                        int m=0;
                        int temp1=i+6;

                        for (int j = i + 6; j < textChars.length; j++) {
                            if(textChars[j]=='课'&&textChars[j+1]=='程'){
                                while (temp1<j){
                                    courseName1[n++]=textChars[temp1];
                                    temp1++;
                                }
                                j=j+2;
                                while (j<textChars.length){
                                    studentName1[m++]=textChars[j];
                                    j++;
                                }
                            }
                        }
                        String courseName2 = String.valueOf(courseName1);//将char数组转为String
                        String studentName2 = String.valueOf(studentName1);//将char数组转为String
                        attributes.addAttribute("courseName",courseName2);
                        attributes.addAttribute("studentName",studentName2);
                        return new RedirectView("/deleteScores");
                    }
                }

        }
        return null;
    }

    public String getMessage(char[] textChars,int i){
        char[] chars = new char[100];
        int n = 0;
        for (int j = i; j < textChars.length; j++) {
            chars[n++] = textChars[j];
        }
        String message = String.valueOf(chars);
        String message2=message.substring(0,n);
        return message2;
    }

    public String getName(char[] textChars,int start,int end){
        char[] chars = new char[100];
        int n=0;
        for (int j = start; j < end; j++) {
            chars[n++] = textChars[j];
        }
        String name = String.valueOf(chars);
        String name2=name.substring(0,n);
        return name2;
    }


}
